#include<iostream>
using namespace std;
#include<ctime>
#include<omp.h>

/*
 * integ_midpoint: 中值法积分
 * 给定区间[a,b]，分割为n份，被积函数为func，返回积分值
 */
double integ_midpoint(double a, double b, long long n, double (*func)(double)){

	double y=0;
	double h=(b-a)/n;

	#pragma omp parallel for reduction(+:y)
		for(long long i=0;i<n;i++){
		       	y += func( a+ (i+0.5)*h);
		}
	y *= h;
	return y;
}

double f(double x){
	return x*x;
}

int main(){

	double a=0,b=1;
	long long n = 1E10;

	time_t t_start = time(NULL);
	cout<<"integ = "<<integ_midpoint(a,b,n,f)<<endl;
	time_t t_end = time(NULL);
	cout<<"Elapsed time: "<<(t_end-t_start)<<" s.\n";

	return 0;
}
